package com.flag.liutao.modules.student.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flag.liutao.common.handler.MyException;
import com.flag.liutao.modules.student.domain.Department;
import com.flag.liutao.modules.student.domain.Grade;
import com.flag.liutao.modules.student.mapper.GradeMapper;
import com.flag.liutao.modules.student.param.GradeParam;
import com.flag.liutao.modules.student.service.IGradeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flag.liutao.modules.student.vo.DepartmentVo;
import com.flag.liutao.modules.student.vo.GradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author godM
 * @since 2022-12-06
 */
@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements IGradeService {
    @Autowired
    private GradeMapper gradeMapper;
    @Override
    public Map<String, Object> getPage(int page, int limit, String keyword) {
        IPage<Grade> iPage = new Page<>(page,limit);
        LambdaQueryWrapper<Grade> lqw = new LambdaQueryWrapper<>();
        lqw.like(Grade::getGradeName,keyword).or().like(Grade::getGradeYear,keyword);
        IPage<Grade> pageDB = gradeMapper.selectPage(iPage, lqw);
        Map<String,Object> map = new HashMap<>();
        List<GradeVo> list = Convert.convert(new TypeReference<List<GradeVo>>() {}, pageDB.getRecords());
        map.put("content",list);
        map.put("total",pageDB.getTotal());
        return map;
    }

    @Override
    public void add(GradeParam param) {
        LambdaQueryWrapper<Grade> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Grade::getGradeName,param.getGradeName());
        Grade department = gradeMapper.selectOne(lqw);
        if(ObjectUtil.isNotNull(department)){
            throw new MyException("年级"+param.getGradeName()+"已存在");
        }
        Grade dept = new Grade();
        dept.setGradeName(param.getGradeName());
        int year = DateUtil.year(param.getGradeYear());
        dept.setGradeYear(year+"");
        gradeMapper.insert(dept);
    }

    @Override
    public void edit(GradeParam param) {

        Grade department = gradeMapper.selectById(param.getId());
        if(ObjectUtil.isNull(department)){
            throw new MyException("该年级不存在");
        }

        department.setGradeName(param.getGradeName());
        department.setGradeYear(DateUtil.year(param.getGradeYear())+"");
        gradeMapper.updateById(department);
    }

    @Override
    public void del(Long id) {
        gradeMapper.deleteById(id);
    }

    @Override
    public List<GradeVo> getGradeList() {

        return Convert.convert(new TypeReference<List<GradeVo>>() {},gradeMapper.selectList(null));
    }


}
